//
//  HamatonCarManagementViewController.swift
//  hamaton-app
//
//  Created by intyt on 2024/4/7.
//

import UIKit
import SnapKit

class HamatonCarManagementListViewController: HamatonBaseViewController {

    lazy var topView: HamatonCarTopView = {
        let topV = HamatonCarTopView(frame: .zero)
        topV.delegate = self
        view.addSubview(topV)
        return topV
    }()
    
    lazy var headerView: HamatonCarListHeaderView = {
        let topV = HamatonCarListHeaderView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 30))
        return topV
    }()
    
    var listArr :[HamatonCarCellModel] = []

    lazy var tableView: UITableView = {
        let tv = UITableView.init(frame: CGRect.zero, style: .plain)
        tv.backgroundColor = .white
        tv.separatorStyle = .none
        tv.delegate = self
        tv.dataSource = self
        tv.register(HamatonCarListTableViewCell.self, forCellReuseIdentifier: "HamatonCarListTableViewCell")
        view.addSubview(tv)
        return tv
    }()
    
    // 测试数据
    func ceshiShuju() {
        let gModel: HamatonModel = HamatonModel(id: "13", name: "高新区第一网格")
        let model: HamatonCarCellModel = HamatonCarCellModel(id: "1", name: "明证", phoneNum: "1515888876555", hamatonModel: gModel, gender: "男", management: "管理员")
        listArr.append(model)
        let model1: HamatonCarCellModel = HamatonCarCellModel(id: "2", name: "明证1", phoneNum: "151588887155", hamatonModel: gModel, gender: "男", management: "管理员")
        listArr.append(model1)
        let model2: HamatonCarCellModel = HamatonCarCellModel(id: "4", name: "数据", phoneNum: "151588887155", hamatonModel: gModel, gender: "男", management: "管理员")
        listArr.append(model2)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.isNavigationBarHidden = true
        // 测试数据
        ceshiShuju()

        // Do any additional setup after loading the view.
        topView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(UIDevice.hamaton_statusBarHeight())
            make.right.left.equalToSuperview()
            make.height.equalTo(64)
        }
        
        tableView.snp.makeConstraints { make in
            make.top.equalTo(topView.snp.bottom).offset(0)
            make.left.right.bottom.equalToSuperview()
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: animated)
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
//        self.navigationController?.setNavigationBarHidden(false, animated: false)
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension HamatonCarManagementViewController: HamatonCarTopViewDelegate {
    func hamatonCarTopViewClick(tag: Int) {
        if tag == 1 {
            self.navigationController?.popViewController(animated: true)
        }else if tag == 3 {
           let vc =  HamatonCarAddViewController()
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    func hamatonCarTopViewTextFieldChange(text: String?) {
        print(text ?? "")
    }
    
    
}


extension HamatonCarManagementViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return listArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "HamatonCarListTableViewCell", for: indexPath) as! HamatonCarListTableViewCell
        if indexPath.row < listArr.count {
            cell.listModel = listArr[indexPath.row]
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 70
    }
    

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 60
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        headerView.numLabel.text = "人员总数：\(listArr.count)"
        return headerView
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row < listArr.count {
            alertShow(model: listArr[indexPath.row])
        }
    }
    
    func alertShow(model: HamatonCarCellModel) {
        let alertController = UIAlertController(title: model.name, message: nil, preferredStyle: .actionSheet)
        let cancelAction = UIAlertAction(title: NSLocalizedString("取消", tableName: "HamatonLocalizable", comment: ""), style: .cancel, handler: nil)
        
        weak var weakSelf = self
        let detailAction = UIAlertAction(title: NSLocalizedString("查看详情", tableName: "HamatonLocalizable", comment: ""), style: .default) { action in
            print("查看详情")
        }
         
        let delectAction = UIAlertAction(title: NSLocalizedString("删除", tableName: "HamatonLocalizable", comment: ""), style: .default) { action in
            weakSelf?.deleteAlertShow(model: model)
        }
         
        alertController.addAction(cancelAction)
         
        alertController.addAction(detailAction)
         
        alertController.addAction(delectAction)
         
        self.present(alertController, animated: true, completion: nil)
    }
    
    func deleteAlertShow(model: HamatonCarCellModel)  {
        let alertController = UIAlertController(title: "确认删除", message: "将\(model.name)从列表中删除？", preferredStyle: .alert)
        let cancelAction = UIAlertAction(title: NSLocalizedString("取消", tableName: "HamatonLocalizable", comment: ""), style: .cancel, handler: nil)
        weak var weakSelf = self
        let delectAction = UIAlertAction(title: NSLocalizedString("删除", tableName: "HamatonLocalizable", comment: ""), style: .destructive) { action in
            weakSelf?.listArr.removeAll(where: { $0.id == model.id })
            weakSelf?.tableView.reloadData()
        }
        alertController.addAction(cancelAction)
         
        alertController.addAction(delectAction)
        self.present(alertController, animated: true, completion: nil)
    }
}

